﻿using System.Linq;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using mal.WP7.GoogleAnalytics.Business.Repository;
using mal.WP7.GoogleAnalytics.Business.Resources;

namespace mal.WP7.GoogleAnalytics.ViewModels
{
    public class EditAccountViewModel : ViewModelBase
    {
        #region Fields
        private AccountViewModel account;
        private RelayCommand testConnexionCommand;
        private bool isLoading = false;
        private bool isLoginSuccess = false;
        private bool isLoginFailed = false;
        #endregion

        #region Construction / Destruction
        public EditAccountViewModel()
        {
            Messenger.Default.Register<TestLoginMessage>(this, this.OnTestLoginMessage);
        }
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties
        public AccountViewModel Account
        {
            get
            {
                return this.account;
            }

            set
            {
                if (this.account != value)
                {
                    this.account = value;
                    RaisePropertyChanged("Account");
                }
            }
        }

        public bool IsLoading
        {
            get
            {
                return this.isLoading;
            }

            set
            {
                if (this.isLoading != value)
                {
                    this.isLoading = value;
                    RaisePropertyChanged("IsLoading");
                }
            }
        }

        public bool IsLoginFailed
        {
            get
            {
                return this.isLoginFailed;
            }

            set
            {
                if (this.isLoginFailed != value)
                {
                    this.isLoginFailed = value;
                    RaisePropertyChanged("IsLoginFailed");
                }
            }
        }

        public bool IsLoginSuccess
        {
            get
            {
                return this.isLoginSuccess;
            }

            set
            {
                if (this.isLoginSuccess != value)
                {
                    this.isLoginSuccess = value;
                    RaisePropertyChanged("IsLoginSuccess");
                }
            }
        }

        public ICommand TestConnexionCommand
        {
            get
            {
                if (this.testConnexionCommand == null)
                {
                    this.testConnexionCommand = new RelayCommand(this.OnTestConnexion);
                }

                return this.testConnexionCommand;
            }
        }
        #endregion

        #region Methods
        public void LoadAccount(string login)
        {
            if (string.IsNullOrEmpty(login))
            {
                this.Account = new AccountViewModel(this, new Account());
            }
            else
            {
                this.Account = new AccountViewModel(this, WebsiteRepository.Instance.Accounts.FirstOrDefault(a => a.Login == login));
            }
        }

        public void Save()
        {
            if (this.Account.IsValid)
            {
                WebsiteRepository.Instance.SaveAccount(this.Account.Account);
            }
        }

        public void Remove()
        {
            WebsiteRepository.Instance.RemoveAccount(this.Account.Account);
        }

        private void OnTestConnexion()
        {
            this.IsLoading = true;
            WebsiteRepository.Instance.TestAccount(this.Account.Account);
        }
        #endregion

        #region Events
        private void OnTestLoginMessage(TestLoginMessage message)
        {
            this.IsLoading = false;

            if (message.LoginSuccess)
            {
                this.IsLoginSuccess = true;
                this.IsLoginFailed = false;
            }
            else
            {
                this.IsLoginSuccess = false;
                this.IsLoginFailed = true;
            }
        }
        #endregion
    }
}
